package cn.itcast.service;

import cn.itcast.dao.PermissionDao;
import cn.itcast.dao.RoleDao;
import cn.itcast.dao.UserDao;
import cn.itcast.pojo.Permission;
import cn.itcast.pojo.Role;
import cn.itcast.pojo.User;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/*
 *Author:TH
 *Dete:2019/10/3020:28
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    //根据用户名查询用户
    public User findByUserneme(String username) {
        User user = userDao.findByUserneme(username);
        if (user==null){
            //用户不存在
            return null;
        }
        //获取用户id查询用户角色
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        //判断角色集合是否为空
        if (roles!=null&&roles.size()>0){
            for (Role role : roles) {
                //获取角色id,查询权限
                Integer roleId = role.getId();
                Set<Permission> permissions =permissionDao.findByRoleId(roleId);
                //判断权限集合是否为空
                if (permissions!=null&&permissions.size()>0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;
    }
}
